package com.qianclass.talklive.app.im;

import java.util.HashMap;
import java.util.Map;

public class AgencyManager {

	public void AddAgency(Integer id, String agency_name)
	{
		_agency_map.put(id, agency_name);
	}

	// 通过机构名获取机构id
	public Integer GetAgencyIdByAgencyName( String agency_name )
	{
		for(Map.Entry<Integer, String> entry : _agency_map.entrySet())
		{
			if( entry.getValue().equals(agency_name) )
			{
				return entry.getKey();
			}
		}
		return -1;
	}

	// 通过机构标识获取机构id
	// 例如组标识:{g:gid@orgName.AgencyName}
	// 或者用户标识:{uid:name@GroupName.AgencyName}
	public Integer GetAgencyIdByIdentify( String agency_identify )
	{
		// 通过机构标识解析机构名称
		try
		{
			int pos = agency_identify.lastIndexOf('.');
			String agency_name = agency_identify.substring(pos + 1, agency_identify.length());
			return GetAgencyIdByAgencyName(agency_name);
		}
		catch( Exception e )
		{
			return -1;
		}
	}

	public String GetAgencyName( Integer id )
	{
		for(Map.Entry<Integer, String> entry : _agency_map.entrySet())
		{
			if( entry.getKey() == id )
			{
				return entry.getValue();
			}
		}
		return null;
	}

	public void Clear()
	{
		_agency_map.clear();
	}

	public String visit_agency()
	{
		String list = new String();
		list = "{";
		for(Map.Entry<Integer, String> entry : _agency_map.entrySet())
		{
			list += "[";
			list += entry.getKey().toString();
			list += ",";
			list += entry.getValue();
			list += "]";
		}
		list += "}";
		return list;
	}

	private Map<Integer,String> _agency_map = new HashMap<Integer,String>();
}
